//
//  CountryViewController.m
//  Qyer
//
//  Created by 赵宇 on 15/9/14.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "CountryViewController.h"
#import "BaseHeader.h"
@interface CountryViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>


@end

@implementation CountryViewController
-(void)dealloc
{
    [_cid release];
    [_photoUrlArr release];
    [_hot_cityArr release];
    [_NDArr release];
    [_cnname release];
    [_enname release];
    [_entryCont release];
    [_titleScroll release];
    [_countryId release];
    [_navView release];
    [_firstView release];
    [_secondView release];
    [_freeTable release];
    [_bigScroll release];
    [_backView release];
    [_entryContLab release];
    [_ennameLab release];
    [_cnnameLab release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
   self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.automaticallyAdjustsScrollViewInsets = NO;
    _pageCount = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    [self getTheData];
    
}
-(void)getTheData
{
    _photoUrlArr = [[NSMutableArray alloc]init];
    _hot_cityArr = [[NSMutableArray alloc]init];
    _NDArr = [[NSMutableArray alloc]init];//new_discount
    NSString *string =[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@&lat=38.88266913415454&lon=121.5397627126178&page=1&track_app_version=6.6&track_deviceid=29DA480A-87F0-4C65-A48A-4B13DE755312", self.cid];
    [AFNblock getdataWithUrlString:string dataBlock:^(id block) {
        [self dealDataWithDic:block];
        
    } netBlock:^(id block) {
        
        [self dealDataWithDic:block];
    }];
    
}
-(void)dealDataWithDic:(NSDictionary *)block
{
    NSDictionary *bigDic = block;
    NSDictionary *dataDic = [bigDic objectForKey:@"data"];
    self.countryId = [dataDic objectForKey:@"id"];
    NSArray *photoArr = [dataDic objectForKey:@"photos"];
    self.cnname= [dataDic objectForKey:@"cnname"];
    self.enname = [dataDic objectForKey:@"enname"];
    self.entryCont = [dataDic objectForKey:@"entryCont"];
    for (int i = 0; i < photoArr.count; i++) {
        NSString *url = [photoArr objectAtIndex:i];
        [_photoUrlArr addObject:url];
    }
    
    NSArray *hotcityArr = [dataDic objectForKey:@"hot_city"];
    for (int i = 0; i < hotcityArr.count; i++) {
        NSDictionary *dic = [hotcityArr objectAtIndex:i];
        hot_countryModel *city = [[hot_countryModel alloc]initWithDictionary:dic];
        [_hot_cityArr addObject:city];
    }
    NSArray *newArr = [dataDic objectForKey:@"new_discount"];
    for (int i = 0; i < newArr.count; i++) {
        NSDictionary *dic = [newArr objectAtIndex:i];
        freeModel *free = [[freeModel alloc]initWithDictionary:dic];
        [_NDArr addObject:free];
    }
    [self setUpBigScroll];
    
}
#pragma mark - 把navigation隐藏,建一个假的nav,并实现上拉nav渐渐出现,然后在nav上加一个button,可以pop回上一个界面
-(void)navigation
{
    _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    _navView.backgroundColor = [UIColor whiteColor];
    _navView.alpha = 0;

    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 30)];
    name.text = self.cnname;
    name.textAlignment = YES;
    name.font = [UIFont systemFontOfSize:15.];
    [_navView addSubview:name];
    
    UILabel *enname = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 30)];
    enname.text = self.enname;
    enname.textAlignment = YES;
    enname.font = [UIFont systemFontOfSize:14.];
    enname.textColor = [UIColor grayColor];
    [_navView addSubview:enname];
    
   UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBut setImage:[UIImage imageNamed:@"whiteback.png"] forState:UIControlStateNormal];
    backBut.frame = CGRectMake(10, 30, 24, 24);
    [backBut addTarget:self action:@selector(backViewClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBut];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"iconfont-31fanhui1"] forState:UIControlStateNormal];
    back.frame = CGRectMake(10, 30, 24, 24);
    [back addTarget:self action:@selector(backViewClick:) forControlEvents:UIControlEventTouchUpInside];
    [_navView addSubview:back];

     [self.view addSubview:_navView];
    
    [_navView release];
    [name release];
    [enname release];
    
}
-(void)backViewClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 创建一个bigScroll,创建3个View,heaView = _titleScroll(轮播图),View1 = label(精彩当地游 查看全部) + CollectionView(4个item,来自local_discount),View2 = label(超值自由行 查看全部) + Tableview(n个cell),然后把heaView和View1和View2放在bigScroll上
-(void)setUpBigScroll
{
    [self creatTitleScroll];
    [self creatFirstViewAndSecondView];
    _bigScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_bigScroll setContentSize:CGSizeMake(0, _titleScroll.frame.size.height+_firstView.frame.size.height+_secondView.frame.size.height)];
    [_bigScroll addSubview:_titleScroll];
    [_bigScroll addSubview:_firstView];
    [_bigScroll addSubview:_secondView];
    [_bigScroll setDelegate:self];

//放在轮播图上的灰色的背景图,为了让文字更清晰的显示
    _backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/5, SCREEN_WIDTH, SCREEN_HEIGHT/5)];
    [_backView setImage:[UIImage imageNamed:@"backViewCell"]];
    [_bigScroll addSubview:_backView];
    
    _entryContLab = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT/5*2 - SCREEN_HEIGHT*0.09, SCREEN_WIDTH - 40, SCREEN_HEIGHT*0.09)];
    _entryContLab.numberOfLines = 2;
    _entryContLab.text = self.entryCont;
    _entryContLab.textColor = [UIColor whiteColor];
    _entryContLab.font = [UIFont systemFontOfSize:14.];
    [_bigScroll addSubview:_entryContLab];
    
    _ennameLab = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT/5*2 - SCREEN_HEIGHT*0.12, SCREEN_WIDTH , SCREEN_HEIGHT*0.045)];
    _ennameLab.font = [UIFont systemFontOfSize:14.];
    _ennameLab.text = self.enname;
    _ennameLab.textColor = [UIColor whiteColor];
    [_bigScroll addSubview:_ennameLab];
    
    _cnnameLab = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREEN_HEIGHT/5*2-SCREEN_HEIGHT*0.17, SCREEN_WIDTH, SCREEN_HEIGHT*0.045)];
    _cnnameLab.font = [UIFont systemFontOfSize:18.];
    _cnnameLab.text = self.cnname;
    _cnnameLab.textColor = [UIColor whiteColor];
    [_bigScroll addSubview:_cnnameLab];
    [self.view addSubview:_bigScroll];
    
    [self navigation];
    
    [_bigScroll release];
    [_backView release];
    [_entryContLab release];
    [_ennameLab release];
    [_cnnameLab release];
}
-(void)creatTitleScroll
{
    _titleScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/5*2)];
    [_titleScroll setContentSize:CGSizeMake((_photoUrlArr.count+2)*SCREEN_WIDTH, 0)];
    [_titleScroll setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
    [_titleScroll setPagingEnabled:YES];
    [_titleScroll setDelegate:self];
    [_titleScroll showsHorizontalScrollIndicator];
    
    for (int i = 0; i < _photoUrlArr.count+2; i++) {
        UIImageView *picV = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT/5*2)];
// 在前面加一张图,和最后一张相同
        if (i == 0) {
            [picV sd_setImageWithURL:[NSURL URLWithString:[_photoUrlArr objectAtIndex:_photoUrlArr.count-1]] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
            
        }
        if (i>0&&i<_photoUrlArr.count+1)
        {
            [picV sd_setImageWithURL:[NSURL URLWithString:[_photoUrlArr objectAtIndex:i-1]] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
            
        }
//最后面也加一张图,和第一张一样
        if (i == _photoUrlArr.count +1)
        {
            [picV sd_setImageWithURL:[NSURL URLWithString:[_photoUrlArr objectAtIndex:0]] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
        }
        [_titleScroll addSubview:picV];
    }
    
    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(titleScrollTime) userInfo:nil repeats:YES];

}
#pragma mark  轮播图实现,每3秒_titleScroll的偏移量增加一个屏幕宽度
-(void)titleScrollTime
{
    [_titleScroll setContentOffset:CGPointMake(_pageCount * SCREEN_WIDTH, 0) animated:YES];
        _pageCount++;

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _titleScroll) {
        // 偏移量/当页宽度 = 当前第几页
        _pageCount = _titleScroll.contentOffset.x / SCREEN_WIDTH;
        if (_pageCount == _photoUrlArr.count + 1) {
            [_titleScroll setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
            
        }
        if (_titleScroll.contentOffset.x <= 0) {
            [_titleScroll setContentOffset:CGPointMake(_photoUrlArr.count * SCREEN_WIDTH, 0) animated:NO];
        }
        
    }
    if (scrollView == _bigScroll)
    {
// nav渐变
        _navView.alpha = 0.01 * scrollView.contentOffset.y;

        if (_bigScroll.contentOffset.y < 0) {
    
            _titleScroll.transform = CGAffineTransformMakeScale((_titleScroll.frame.size.height-_bigScroll.contentOffset.y)/_titleScroll.frame.size.height, (_titleScroll.frame.size.height-_bigScroll.contentOffset.y)/_titleScroll.frame.size.height);
            _titleScroll.center = CGPointMake(SCREEN_WIDTH/2, _titleScroll.frame.size.height/2+_bigScroll.contentOffset.y);
        
            _cnnameLab.frame = CGRectMake(20, _titleScroll.frame.size.height-SCREEN_HEIGHT*0.17 + _bigScroll.contentOffset.y, SCREEN_WIDTH - 40, 30);
            
            _ennameLab.frame = CGRectMake(20, _titleScroll.frame.size.height - SCREEN_HEIGHT*0.12+_bigScroll.contentOffset.y, SCREEN_WIDTH , SCREEN_HEIGHT*0.045);
            
            _entryContLab.frame = CGRectMake(20, _titleScroll.frame.size.height - SCREEN_HEIGHT*0.09+_bigScroll.contentOffset.y, SCREEN_WIDTH - 40, SCREEN_HEIGHT*0.09);
            
            _backView.frame = CGRectMake(0, _titleScroll.frame.size.height/2+_bigScroll.contentOffset.y, SCREEN_WIDTH, _titleScroll.frame.size.height / 2);
        }
    }
}

-(void)creatFirstViewAndSecondView
{
    _firstView = [[UIView alloc]initWithFrame:CGRectZero];
    _secondView =[[UIView alloc]initWithFrame:CGRectZero];
// 判断热门城市数量,因为每行放两个,奇数的时候高度得×_hot_cityArr.count/2+1
    if (_hot_cityArr.count != 0)
    {
        if (_hot_cityArr.count % 2 == 0) {
            _number = (int)_hot_cityArr.count/2;
        }else{
            _number = (int)_hot_cityArr.count/2+1;
        }
            
        _firstView.frame =CGRectMake(0, SCREEN_HEIGHT/5*2, SCREEN_WIDTH, _number*SCREEN_WIDTH/25 * 6.6+0.13*SCREEN_HEIGHT);
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/4, 0.1*SCREEN_WIDTH)];
        title.text = [NSString stringWithFormat:@"%@城市",self.cnname];
        title.font = [UIFont systemFontOfSize:15.];
        title.textColor = [UIColor grayColor];
        [_firstView addSubview:title];
        
        UIButton *firstBut = [UIButton buttonWithType:UIButtonTypeCustom];
        firstBut.frame = CGRectMake(SCREEN_WIDTH/3*2, 0, SCREEN_WIDTH/3, 0.1*SCREEN_WIDTH);
        [firstBut setTitle:@"查看全部>" forState:UIControlStateNormal];
        [firstBut addTarget:self action:@selector(showAllCountry) forControlEvents:UIControlEventTouchUpInside];
        firstBut.titleLabel.font = [UIFont systemFontOfSize:15.];
        [firstBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_firstView addSubview:firstBut];
        
        UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0.1*SCREEN_WIDTH, SCREEN_WIDTH, .6)];
        line1.backgroundColor = [UIColor grayColor];
        [_firstView addSubview:line1];
        
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.itemSize = CGSizeMake(SCREEN_WIDTH/7*3.1, SCREEN_WIDTH/25 * 6.6);
        flow.sectionInset = UIEdgeInsetsMake(SCREEN_WIDTH/35, SCREEN_WIDTH/25, SCREEN_WIDTH/35, SCREEN_WIDTH/25);
        
        [flow setMinimumInteritemSpacing:10];
        [flow setMinimumLineSpacing:10];
        
        UICollectionView *collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 42, SCREEN_WIDTH, _number*SCREEN_WIDTH/25 * 6.6+0.12*SCREEN_WIDTH) collectionViewLayout:flow];
        collection.backgroundColor = [UIColor whiteColor];
        [collection registerClass:[ListCollectionViewCell class] forCellWithReuseIdentifier:@"List"];
        [collection setDelegate:self];
        [collection setDataSource:self];
        
        [_firstView addSubview:collection];
        
        [title release];
        [line1 release];
        [collection release];
        
     
    }if ( _NDArr.count != 0 ) {
        _freeTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, _NDArr.count*(SCREEN_HEIGHT/ 5 + 10)) style:UITableViewStylePlain];
        [_freeTable setDelegate:self];
        [_freeTable setDataSource:self];
        
        _secondView.frame = CGRectMake(0, _firstView.frame.size.height+_titleScroll.frame.size.height, SCREEN_WIDTH, _freeTable.frame.size.height + 60);
        
        UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH,.6)];
        line2.backgroundColor = [UIColor grayColor];
        [_secondView addSubview:line2];
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 40)];
        title.text = @"超值自由行";
        title.font = [UIFont systemFontOfSize:15.];
        title.textColor = [UIColor grayColor];
        [_secondView addSubview:title];
        
        UIButton *secBut = [UIButton buttonWithType:UIButtonTypeCustom];
        secBut.frame = CGRectMake(SCREEN_WIDTH/3*2, 0, SCREEN_WIDTH/3, 40);
        [secBut setTitle:@"查看全部>" forState:UIControlStateNormal];
        [secBut addTarget:self action:@selector(allFreeTra) forControlEvents:UIControlEventTouchUpInside];
        secBut.titleLabel.font = [UIFont systemFontOfSize:15.];
        [secBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_secondView addSubview:secBut];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, .6)];
        line.backgroundColor = [UIColor grayColor];
        [_secondView addSubview:line];
        [_secondView addSubview:_freeTable];
        
        [_freeTable release];
        [line2 release];
        [title release];
        [line release];

    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SlideViewController *webView = [[SlideViewController alloc]init];
    freeModel *FModel = [_NDArr objectAtIndex:indexPath.row];
    
    NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/lastminute/get_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=1&source=QYCountryDetailViewController&track_user_id=6695170&v=1",FModel.cid];
    
    [AFNblock getdataWithString:string block:^(id block) {
        NSDictionary *bigDic = block;
        NSDictionary *dataDic = [bigDic objectForKey:@"data"];
        NSString *qyer_url = [dataDic objectForKey:@"qyer_url"];
        
        webView.url = qyer_url;
        [self.navigationController pushViewController:webView animated:YES];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _NDArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *freeTCellid = @"freeTCellID";
    FreedomTableViewCell *ftCell = [tableView dequeueReusableCellWithIdentifier:freeTCellid];
    if (!ftCell) {
        ftCell = [[FreedomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:freeTCellid];
    }
    ftCell.selectionStyle = UITableViewCellSelectionStyleNone;
    freeModel *freeM = [_NDArr objectAtIndex:indexPath.row];
    [ftCell getValueWithModel:freeM];
    return ftCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT/ 5 + 10;
}
#pragma mark - UICollectionView的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _hot_cityArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ListCollectionViewCell *listCollectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"List" forIndexPath:indexPath];
    hot_countryModel *mol = [_hot_cityArr objectAtIndex:indexPath.row];
    [listCollectionCell setUpWithModel:mol];
    return listCollectionCell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllCountViewController *allCountCountry = [[AllCountViewController alloc]init];
    hot_countryModel *country = [_hot_cityArr objectAtIndex:indexPath.row];
    NSString *str = country.cid;
    allCountCountry.urlStr = str;
    
    [self.navigationController pushViewController:allCountCountry animated:YES];
    
}
// 热门城市,查看全部
-(void)showAllCountry
{
    WholeCountryViewController *wCountry = [[WholeCountryViewController alloc]init];
 
    wCountry.url = self.cid;
    [self.navigationController pushViewController:wCountry animated:YES];
}
-(void)allFreeTra
{
    WholeFreedomViewController *wft = [[WholeFreedomViewController alloc]init];
// 网址里,如果是在国家页面点,type就是1,城市页面点超值自由行的查看全部,type就是2
    wft.type = 1;
    wft.left = YES;
    wft.urlStr = self.cid;
    [self.navigationController pushViewController:wft animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
